
cc.Class({
    extends: cc.Component,

    properties: {
        block1_Pre:{
            type:cc.Prefab,
            default:null
        },
        block2_Pre:{
            type:cc.Prefab,
            default:null
        },
        block3_Pre:{
            type:cc.Prefab,
            default:null
        },
        block4_Pre:{
            type:cc.Prefab,
            default:null
        },
        block5_Pre:{
            type:cc.Prefab,
            default:null
        },
        block0_Pre:{
            type:cc.Prefab,
            default:null
        },
        score_Label:{
            type:cc.Label,
            default:null
        },
        pass_Pre:{
            type:cc.Prefab,
            default:null
        },
        addScore_Pre:{
            type:cc.Prefab,
            default:null
        }
    },

    onLoad () {
        window.map = this;
        this.level = 1;  //当前关卡
        let lev = cc.sys.localStorage.getItem("level");
        if(lev){
            this.level = parseInt(lev);
        }
        this.score_Label.getComponent(cc.Label).string = 10;
        this.mapNode1 = [["0","0","0","4","4","0","0","0"],
                        ["0","0","0","4","4","0","0","0"],
                        ["0","0","0","4","4","0","0","0"],
                        ["1","1","1","0","0","2","2","2"],
                        ["1","1","1","0","0","2","2","2"],
                        ["0","0","0","3","3","0","0","0"],
                        ["0","0","0","3","3","0","0","0"],
                        ["0","0","0","3","3","0","0","0"]];

        this.mapNode2 = [["0","0","0","4","4","0","3","0"],
                        ["0","0","0","4","4","0","3","0"],
                        ["0","0","0","4","4","0","3","0"],
                        ["1","1","1","0","0","2","3","2"],
                        ["1","1","1","0","0","2","3","2"],
                        ["0","0","0","3","3","0","3","0"],
                        ["0","0","0","3","3","0","3","0"],
                        ["0","0","0","3","3","0","3","0"]];
        this.initMap();
        // this.pass();
    },
    initMap(){
        let mapnode = null;
        switch (this.level) {
            case 1:
                mapnode = this.mapNode1;
                break;
            case 2:
                mapnode = this.mapNode2;
                break;
            default:
                break;
        }
        for(let i=0;i<mapnode.length;i++){
            for(let j=0;j<mapnode[i].length;j++){
                if(mapnode[i][j] == 1){
                    this.addMyChild(this.block1_Pre);
                }else if(mapnode[i][j] == 2){
                    this.addMyChild(this.block2_Pre);
                }else if(mapnode[i][j] == 3){
                    this.addMyChild(this.block3_Pre);
                }else if(mapnode[i][j] == 4){
                    this.addMyChild(this.block4_Pre);
                }else if(mapnode[i][j] == 5){
                    this.addMyChild(this.block5_Pre);
                }else{
                    this.addMyChild(this.block0_Pre);
                }
            }
        }
    },
    addMyChild(blockType){
        let blockNode = cc.instantiate(blockType);
        this.node.getChildByName("old").addChild(blockNode);
    },
    check(){
        let children1 = this.node.getChildByName("old").children;
        this.myarr=new Array();
        for(let i=0;i<8;i++){
            this.myarr[i]=new Array();
            for(let j=0;j<8;j++){
                this.myarr[i][j]=children1[ i * 8 + j];
            }
        }
        let columnNum = this.checkColumn();
        let lineNum = this.checkLine();
        for(let i = 0;i<columnNum.length;i++){
            this.delColumn(columnNum[i]);
        }
        for(let i = 0;i<lineNum.length;i++){
            this.delLine(lineNum[i]);
        }
        let self = this;
        setTimeout (function(){
            self.gameOver();
        },1000);
    },
    checkColumn(){//检查列
        let columnNum = new Array();
        for(let i = 0 ; i < this.myarr.length ; i ++){
            let num = 0;
            for(let j = 0 ; j < this.myarr.length ; j ++){
                if(this.myarr[j][i].getComponent(cc.Sprite).node._components[0].spriteFrame){
                    num ++;
                }
                if(num == 8){
                    // this.delColumn(i);
                    columnNum.push(i);
                }
            }
        }
        return columnNum;
    },
    checkLine(){//检查行
        let lineNum = new Array();
        for(let i = 0 ; i < this.myarr.length ; i ++){
            let num = 0;
            for(let j = 0 ; j < this.myarr[i].length ; j ++){
                // console.log(this.myarr[i][j].getComponent(cc.Sprite).node._components[0].spriteFrame)
                if(this.myarr[i][j].getComponent(cc.Sprite).node._components[0].spriteFrame){
                    num ++;
                }
                if(num == 8){
                    // this.delLine(i);
                    lineNum.push(i);
                }
            }
        }
        return lineNum;
    },
    delLine(num){
        for(let j = 0 ; j < this.myarr[num].length ; j ++){
            // this.myarr[num][j].destroy();
            var sto = cc.scaleTo(0.3,0);   //不管scale为多少,直接设置为节点原始大小的5倍
            this.myarr[num][j].runAction(sto);
            let self = this;
            setTimeout (function(){
                self.myarr[num][j].getComponent(cc.Sprite).spriteFrame = null;
            },1000);
        }
        this.addScore();
    },
    delColumn(num){
        for(let j = 0 ; j < this.myarr.length ; j ++){
            var sto = cc.scaleTo(0.3,0);   //不管scale为多少,直接设置为节点原始大小的5倍
            this.myarr[j][num].runAction(sto);
            let self = this;
            setTimeout (function(){
                self.myarr[j][num].getComponent(cc.Sprite).spriteFrame = null;
            },1000);
        }
        this.addScore();
    },
    gameOver(){
        for(let i = 0 ; i < this.myarr.length ; i ++){
            for(let j = 0 ; j < this.myarr[i].length ; j ++) {
                if(this.myarr[i][j].getComponent(cc.Sprite).spriteFrame != null){
                    return;
                }
            }
        }
        this.pass();
    },
    addScore(){//加分
        let myDrugNode = cc.instantiate(this.addScore_Pre);
        this.score_Label.getComponent(cc.Label).string = parseInt(this.score_Label.getComponent(cc.Label).string)+700;
        myDrugNode.setPosition(0,0);
        myDrugNode.parent = this.node;
    },
    pass(){
        cc.sys.localStorage.setItem("level", parseInt(this.level)+1);
        let blockNode = cc.instantiate(this.pass_Pre);
        // this.node.addChild(blockNode);
        blockNode.parent = this.node.parent;
        blockNode.setPosition(cc.v2(0,-960));
    },


    // update (dt) {},
});
